/*
 * @Author: ZhanXie
 * @Date: 2019-12-04 17:31:46
 * @LastEditors  : ZhanXie
 * @LastEditTime : 2019-12-28 17:39:47
 * @Description: user 路由
 * @FilePath: /node-demo/blog-express/routes/user.js
 */
var express = require('express');
var router = express.Router();
const { SuccessModel, ErrorModel } = require('../model/resModel')
const { login } = require('../controller/user')

router.post('/login', function (req, res, next) {
  const { username, password } = req.body
  // const { username, password } = req.query
  const result = login(username, password)
  return result.then(data => {
    if (data.username) {
      // 设置 session
      req.session.username = data.username
      req.session.realname = data.realname
      res.json(new SuccessModel(data))
      return 
    }
    res.json(new ErrorModel('登录失败'))
    return 
  })
});

// router.get('/login-test', function (req, res, next) {
//   console.log(req.session)
//   if(req.session.username) {
//     res.json(new SuccessModel('已成功'))
//   } else {
//     res.json(new ErrorModel('失败'))
//   }
// }) 

module.exports = router;